/**************************************************************************************

 *  COSC 603 Project 2
 *  Copyright (C) 2012 Jamie Xu

 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *************************************************************************************/
package com.example.stutterwebapp.server;

import com.example.stutterwebapp.client.GreetingService;
import com.example.stutterwebapp.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {
	public static String [] line; // Each element in this array stores a single line from the input.
	public static String output;  // Stores the output that is returned by the server. IE: the stutter, if ny
	
	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidText(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Text must be at least 10 characters long");
		}
		
		// There was an issue after deployment where everything would be saved on the server.
		// The next few lines prevents that from occuring by clearing everything.
		line = null;
		output = "";
		
		// Put each line of the input in a seperate array element
   		line = input.split("[\\r\\n]+");
   		
		checkStutter();
		
		return output;
	}
	
	/*	
	/**
     * Check stutter.
     * 
     * Reads the input file one line, then one word at a time.
     * Looks for repeated words and informs users.
	 *
     */
   	public static void checkStutter() {
   		String lastWord = null;
   		
   		for(int i = 0; i < line.length; i++) {
	   		// Remove all non alpha-numeric characters
	   		line[i] = line[i].replaceAll ("[^a-zA-Z0-9 ]", "");
	   		
	   		// Convert the input to lower case
			line[i] = line[i].toLowerCase();
			
			// Splits the line into words and puts in an array of strings
			String[] words = line[i].split (" ");
			
			// Compare with the last word on the previous line
			if (words [0].equals (lastWord)) {
				output += "Repeated word on line number " + (i+1) + ": " + words[0] + "<br />";
			}
	   		
			// Stop before the end, nothing to compare the last word with
			for (int j = 0; j < (words.length-1); j++){
				// Check to see if the current and subsequent words are the same
				if (words [j].equals (words [j+1])) {
					output += "Repeated word on line number " + (i+1) + ": " + words [j] + "<br />";
				}
			}
			
			// Save last word in the line
			lastWord = words [words.length-1];
   		}
   	}
	
}
